﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OL.FrameCore.Application.Article;
using OL.FrameCore.Application.Article.Dto;
using OL.FrameCore.WebUI.ViewModels.Article;

namespace OL.FrameCore.WebUI.Controllers
{
    public class ArticleController : ControllerBase
    {
        IArticleService _articleService;
        public ArticleController(IArticleService articleService)
        {
            _articleService = articleService;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取操作日志列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetArticleList(GetArticleListViewRequest request)
        {
            var serviceRequest = ToMap<GetArticleListViewRequest, GetArticleListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;

            var serviceResult = _articleService.GetArticleList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };

            return Json(result);
        }

        /// <summary>
        /// 添加或修改部门页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AddOrUpdate(int? id)
        {
            GetArticleResponse model = new GetArticleResponse();
            if (id != null)
            {
                model = _articleService.GetArticle(id.Value);
            }
            return View(model);
        }

        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddArticle(AddArticleViewRequest request)
        {
            //验证模型数据
            if (IsNotValidModel)
            {
                return FailResult(ModelErrorMessage);
            }

            var serviceRequest = ToMap<AddArticleViewRequest, AddArticleRequest>(request);
            serviceRequest.CreateId = UserId;
            var result = _articleService.AddArticle(serviceRequest);
            return Json(result);
        }

        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateArticle(UpdateArticleViewRequest request)
        {
            //验证模型数据
            if (IsNotValidModel)
            {
                return FailResult(ModelErrorMessage);
            }

            var serviceRequest = ToMap<UpdateArticleViewRequest, UpdateArticleRequest>(request);
            serviceRequest.UpdateId = UserId;
            var result = _articleService.UpdateArticle(serviceRequest);
            return Json(result);
        }

        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteArticle(int id)
        {
            var result = _articleService.DeleteArticle(id, UserId);
            return Json(result);
        }


        public IActionResult Demo()
        {
            return View();
        }
    }
}
